using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using DTO;

namespace DAO
{
    public class HangXeDAO
    {
        public IList DanhSach()
        {
            SqlConnection connect = SqlDataAccessHelper.ConnectSQL();
            SqlCommand command = connect.CreateCommand();
            command.CommandText = "SELECT * FROM HANGXE";
            SqlDataReader dr = command.ExecuteReader();
            IList list = new ArrayList();
            while(dr.Read())
            {
                HangXeDTO hangxe = new HangXeDTO();
                hangxe.MaHangXe = (int)dr["MaHangXe"];
                hangxe.TenHangXe = (string)dr["TenHangXe"];
                list.Add(hangxe);
            }
            dr.Close();
            connect.Close();
            HangXeDTO[] listHangXe = new HangXeDTO[list.Count];
            list.CopyTo(listHangXe,0);
            return listHangXe;
        }

        public IList DanhSach(int MaXe)
        {
            SqlConnection connect = SqlDataAccessHelper.ConnectSQL();
            SqlCommand command = connect.CreateCommand();
            command.CommandText = "SELECT HANGXE.MaHangXe,HANGXE.TenHangXe FROM HANGXE,XE WHERE XE.MaHangXe = HANGXE.MaHangXe AND XE.MaXe = ?";
            command.Parameters.Add("@MaXe", SqlDbType.Int);
            command.Parameters[0].Value = MaXe;
            SqlDataReader dr = command.ExecuteReader();
            IList list = new ArrayList();
            while(dr.Read())
            {
                HangXeDTO hangxe = new HangXeDTO();
                hangxe.MaHangXe = (int)dr["MaHangXe"];
                hangxe.TenHangXe = (string)dr["TenHangXe"];
                list.Add(hangxe);
            }
            dr.Close();
            connect.Close();
            HangXeDTO[] listHangXe = new HangXeDTO[list.Count];
            list.CopyTo(listHangXe, 0);
            return listHangXe;
        }

        #region Retrieving
        public static List<HangXeDTO> SelectHangXeAll()
        {
            List<HangXeDTO> list = new List<HangXeDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectHangXeAll");
                foreach(DataRow dr in dt.Rows)
                {
                    HangXeDTO hangxe = new HangXeDTO();
                    hangxe.MaHangXe = int.Parse(dr["MaHangXe"].ToString());
                    hangxe.TenHangXe = dr["TenHangXe"].ToString();
                    list.Add(hangxe);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static HangXeDTO SelectHangXeByMa(int mahangxe)
        {
            HangXeDTO hangxe = new HangXeDTO();
            try
            {
                //Create list SqlParameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaHangXe", mahangxe));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectHangXeByMa", sqlParams);
                DataRow dr = dt.Rows[0];
                hangxe.MaHangXe = int.Parse(dr["MaHangXe"].ToString());
                hangxe.TenHangXe = dr["TenHangXe"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return hangxe;
        }

        public static bool CheckHangXeByMa(int mahangxe)
        {
            bool result = false;
            try
            {
                //Create list Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaHangXe", mahangxe));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectHangXeByMa", sqlParams);
                if(dt.Rows.Count == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}
